package br.com.felipeab.dexter.bean;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import br.com.felipeab.dexter.bo.PersistenceService;
import br.com.felipeab.dexter.domain.User;
import br.com.felipeab.dexter.expection.TransactionException;
import br.com.felipeab.dexter.faces.CrudController;
import br.com.felipeab.dexter.util.StringHelper;

/**
 * Classe UserBean
 * 
 * @author Felipe A. Braga <br>
 *         18/06/2012 01:41:45<br>
 * 
 */
@ViewScoped
@ManagedBean(name = "userBean")
public class UserBean extends CrudController<User> {

	private static final long serialVersionUID = -4105295686876914116L;

	private User domain;
	private String passwordConfirm;
	private PersistenceService persistenceService;

	public UserBean() {
		System.out.println("Construtor");
	}

	@PostConstruct
	public void init() {
		System.out.println("Depois do construtor");
	}

	@Override
	protected PersistenceService getPersistenceService() {
		if (persistenceService == null) {
			persistenceService = new PersistenceService();
		}
		return persistenceService;
	}

	@Override
	public User getDomain() {
		if (domain == null) {
			domain = new User();
		}
		return domain;
	}

	@Override
	public void setDomain(User domain) {
		this.domain = domain;
	}

	@Override
	protected boolean validate() {
		if (!getDomain().getPassword().equals(getPasswordConfirm())) {
			return true;
		}
		return true;
	}

	@Override
	protected void save() throws TransactionException {
		getDomain().setPassword(StringHelper.encryptText(getDomain().getPassword()));
		super.save();
	}

	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}
}
